﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AirControlATV.Core;
using Microsoft.Phone.Net.NetworkInformation;
using System.Net;
using System.IO;
using System.Windows;
using AirControlATV.Settings;

namespace AirControlATV.Model
{
    public class AirControlCommand
    {
        private static readonly AirControlATVSettings _settings;

        private string _userFriendlyText;
        private AirControlCommandType _airControlCommandType;

        public event AirControlATV.Controls.AirControlButton.CommandResultEventHandler CommandResult;

        public string CommandText { get; private set; }

        public string LongPressCommandText { get; private set; }

        public bool IsRepeatableCommand { get; private set; }

        public string UserFriendlyText
        {
            get
            {
                if (_userFriendlyText == null)
                    _userFriendlyText = _airControlCommandType.GetDisplayValue();
                return _userFriendlyText;
            }
        }

        static AirControlCommand()
        {
            _settings = (AirControlATVSettings)App.Current.Resources["AirControlSettings"];
        }

        public AirControlCommand(AirControlCommandType airControlCommandType)
        {
            _airControlCommandType = airControlCommandType;
            SetupAirControlCommand();
        }

        public async void RunCommand()
        {
            try
            {
                var response = await HttpRequest(App.AppleTVAddress + CommandText);
                CommandResult(response);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public async void RunLongPressCommand()
        {
            var response = await HttpRequest(App.AppleTVAddress + LongPressCommandText);
            CommandResult(response);
        }

        public static async Task<string> HttpRequest(string uriString)
        {
            string received;
            HttpWebRequest request = HttpWebRequest.CreateHttp(uriString);
            request.Headers[HttpRequestHeader.IfModifiedSince] = DateTime.Now.ToString();

            using (var response = (HttpWebResponse)(await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null)))
            {
                using (var responseStream = response.GetResponseStream())
                {
                    using (var sr = new StreamReader(responseStream))
                    {
                        received = await sr.ReadToEndAsync();
                    }
                }
            }

            return received;
        }

        private void SetupAirControlCommand()
        {
            switch (_airControlCommandType)
            {
                case AirControlCommandType.NowPlaying:
                    CommandText = "npx";
                    break;
                case AirControlCommandType.Menu:
                    CommandText = "remoteAction=1";
                    LongPressCommandText = "remoteAction=2";
                    break;
                case AirControlCommandType.ArrowUp:
                    CommandText = "remoteAction=3";
                    IsRepeatableCommand = true;
                    break;
                case AirControlCommandType.ArrowDown:
                    CommandText = "remoteAction=4";
                    IsRepeatableCommand = true;
                    break;
                case AirControlCommandType.Select:
                    CommandText = "remoteAction=5";
                    LongPressCommandText = "remoteAction=22";
                    break;
                case AirControlCommandType.ArrowLeft:
                    CommandText = "remoteAction=6";
                    IsRepeatableCommand = true;
                    break;
                case AirControlCommandType.ArrowRight:
                    CommandText = "remoteAction=7";
                    IsRepeatableCommand = true;
                    break;
                case AirControlCommandType.Sleep:
                    CommandText = "sleep=1";
                    break;
                case AirControlCommandType.Wake:
                    CommandText = "wake=0";
                    break;
                case AirControlCommandType.Netflix:
                    CommandText = "plugin=netflix";
                    break;
                case AirControlCommandType.MyMedia:
                    CommandText = "plugin=com.apple.frontrow.appliance.media/LocalSources";
                    break;
                case AirControlCommandType.Settings:
                    CommandText = "plugin=com.apple.frontrow.appliance.settings";
                    break;
                case AirControlCommandType.Reboot:
                    CommandText = "kf=1";
                    break;
                case AirControlCommandType.NowPlaying2:
                    CommandText = "np=0";
                    break;
                case AirControlCommandType.EnterText:
                    CommandText = "enterText=yourtext";
                    break;
                case AirControlCommandType.FlagStaff:
                    CommandText = "com.apple.frontrow.appliance.internet.flagstaff";
                    break;
                case AirControlCommandType.Vimeo:
                    CommandText = "com.apple.frontrow.appliance.internet.vimeo";
                    break;
                case AirControlCommandType.NBA:
                    CommandText = "com.apple.frontrow.appliance.internet.nba";
                    break;
                case AirControlCommandType.MovieTrailers:
                    CommandText = "com.apple.frontrow.appliance.internet.movie-trailers";
                    break;
                case AirControlCommandType.WSJ:
                    CommandText = "com.apple.frontrow.appliance.internet.wsj";
                    break;
                case AirControlCommandType.NHL:
                    CommandText = "com.apple.frontrow.appliance.internet.NHL";
                    break;
                case AirControlCommandType.Photos:
                    CommandText = "com.apple.frontrow.appliance.internet.internet-photos";
                    break;
                case AirControlCommandType.DotMAC:
                    CommandText = "com.apple.frontrow.appliance.internet.dot-mac";
                    break;
                case AirControlCommandType.Flickr:
                    CommandText = "com.apple.frontrow.appliance.internet.flickr";
                    break;
                case AirControlCommandType.PhotoStream:
                    CommandText = "com.apple.frontrow.appliance.internet.photo-stream";
                    break;
                case AirControlCommandType.YouTube:
                    CommandText = "com.apple.frontrow.appliance.internet.internet-youtube";
                    break;
                case AirControlCommandType.Podcasts:
                    CommandText = "com.apple.frontrow.appliance.internet.internet-podcasts";
                    break;
                case AirControlCommandType.MoviesMovieTrailers:
                    CommandText = "com.apple.frontrow.appliance.movies.movie-trailers";
                    break;
                case AirControlCommandType.InternetNetflix:
                    CommandText = "com.apple.frontrow.appliance.internet.netflix";
                    break;
                default:
                    CommandText = "remoteAction=" + (int)_airControlCommandType;
                    break;
            }
        }
    }
}
